
* This do file prepares data for analysis from a cumulative data file containing ESS (1, 2, 3, 4, 5, 6) 
* Find documentation for the data here: http://www.europeansocialsurvey.org/data/

* Opening the cumulative ESS1-6 file

use ESS1-6_cumulative_e01_1, clear

* Keeping countries included in the analysis

keep if cntry=="AT" | cntry=="BE" | cntry=="DE" | cntry=="DK" | cntry=="ES" | cntry=="FI" | cntry=="FR" | ///
  cntry=="GB" | cntry=="GR" | cntry=="IE" | cntry=="NL" | cntry=="NO" | cntry=="PT" | cntry=="SE"

  
* Recoding variables for the analysis  

* Redistribution Preferences

* Create 3 alternative variables for redistribution preferences

recode gincdif (1 2 =1 "agree or strongly agree") (nonmissing = 0 "neither agree nor disagree, disagree, strongly disagree"), gen (redpref1)
label variable redpref1 "redistribution preferences 1"

recode gincdif (1 =1 "strongly agree") (nonmissing = 0 "agree, neither agree nor disagree, disagree, strongly disagree"), gen (redpref2)
label variable redpref2 "redistribution preferences 2"

recode gincdif (1 =5 "strongly agree") (2 = 4 "agree") (3=3 "neither agree nor disagree") (4=2 "disagree") (5=1 "strongly disagree"), gen (redpref3)
replace redpref3=. if gincdif>5

* Age

gen year=2002
replace year=2004 if essround==2
replace year=2006 if essround==3
replace year=2008 if essround==4
replace year=2010 if essround==5
replace year=2012 if essround==6

recode yrbrn (7777/9999=.)
gen age = 999999
replace age =  year - yrbrn

* Gender

recode gndr (1=0)(2=1)(9=.),gen(gender)

* Create religious attendance variable

recode rlgatnd (1 2 3 = 1 "every day, more than once a week, once a week") (nonmissing = 0 "less than once a week"), gen (religion)
label variable religion "how often attend religious services"

* Education

recode eduyrs (77/99=.), gen (education)
label variable education "years of education"

* Union membership

recode mbtru (1 =1 "yes, currently") (nonmissing = 0 "no, or yes previously"), gen (union)
label variable union "union membership2"

* Ideology as self-placement on left-right scale
* The question is self-placement in left-right scale from 0 (extreme left) to 10 (extreme right) 

replace lrscale=. if lrscale>10

* Fear of crime

recode aesfdrk (7 8 9 = .), gen(fear)

* ===========================
* =   Transfer classes      =
* ===========================
*Transfer class : coding Gelissen
// 1 working  2 unempl  3 retired/disabled  4 Not in LF
recode mnactic (1 = 1) (3 4 =2) (5 6 =3)  (2 7 8 9=4) (77 88 99= .),gen(transfer)

* Recode variable capturing living in urban area

recode domicil (7 8 9=.)

* Recoding income variable:
* this file generates an income variable that is the midpoint for the range of the income categorical response
* uses Hout (2004) to define top-incomes, and converts income variable to PPP (US dollars of 2010)

do income 

* Class:
* this file generates a class variable which is an international comparative version of the 
* European Socio-economic Classification (based on ISCO minor groups)

do class

* Create vote variables

do vote

recode ipeqopt (7 8 9 =.)

* Creating macro foreign population variable from OECD International migration database: 
* https://data.oecd.org/migration/foreign-born-population.htm (downloaded 17/07/2016)
* Total foreign-born population as % of population
* For all countries, data for 2014 is from 2013 (not used)
* Portugal 2012 data is from 2011
* Greece 2002 data is from 2001 (not used)

merge m:1 cntry year using forpop
drop if _merge==2
drop _merge

* Creating macro foreign-born population variable from OECD International migration database: 

merge m:1 cntry year using fstock
drop if _merge==2
drop _merge

* Creating macro foreign-born unemployment variable from OECD International migration database: 

merge m:1 cntry year using funemp
drop if _merge==2
drop _merge

* Make Greece forpop measure into non-nationals rather than foreign born

replace forpop=forstock2 if cntry=="GR"

* Creating macro native-born unemployment variable from OECD International migration database: 

merge m:1 cntry year using natunemp
drop if _merge==2
drop _merge

* Create foreign born unemployed as % or total population

gen forunpop=forstock1*(forpart/100)*(forunemp/100)

* Create foreign born as % of poor (under country-year mean) from ESS surveys
* Foreign born defined as answering "no" to question of being born in the country

gen undermean=.
replace undermean=1 if income_ppp2010<mpppincome // All under mean

sort country essround
by country essround: egen cntundermean=count(undermean)

gen forundermean=.
replace forundermean=1 if income_ppp2010<mpppincome & brncntr==2 // Foreign born under mean

by country essround: egen cntforundermean=count(forundermean)

gen forperpoor=(cntforundermean/cntundermean)*100

* Create self-defined ethnic minority as % of poor (under country-year mean) from ESS surveys
* Ethnic minority defined as answering "yes" to question of belonging to minority ethnic group in 
* ESS round country 


gen ethundermean=.
replace ethundermean=1 if income_ppp2010<mpppincome & blgetmg==1 // Ethnic minority under mean

by country essround: egen cntethundermean=count(ethundermean)

gen ethperpoor=(cntethundermean/cntundermean)*100

* Creating macro gini variables from Eurostat 
* gini = Gini coefficient of equivalised disposable income - EU-SILC survey
* ginibefore = Gini coefficient of equivalised disposable income before social transfers (pensions excluded from social transfers)
* http://ec.europa.eu/eurostat/data/database
* Downloaded 7/23/2016

merge m:1 cntry year using gini
drop if _merge==2
drop _merge

* Creating macro social spending variable from OECD 
* This is total, old age and survivor spending measured: 
* Per head, at constant prices (2005) and constant PPPs (2005), in US dollars
* And as % of Gross Domestic Product
* Downloaded from OECD.Stat 24/7/2016

merge m:1 cntry year using oecdsoc
drop if _merge==2
drop _merge

* Creating occupational unemployment rates from Rehm (2009)

gen cou="BEL" if cntry=="BE"
replace cou="DNK" if cntry=="DK"
replace cou="FIN" if cntry=="FI"
replace cou="FRA" if cntry=="FR"
replace cou="DEU" if cntry=="DE"
replace cou="GRC" if cntry=="GR"
replace cou="IRL" if cntry=="IE"
replace cou="NLD" if cntry=="NL"
replace cou="PRT" if cntry=="PT"
replace cou="ESP" if cntry=="ES"
replace cou="SWE" if cntry=="SE"
replace cou="GBR" if cntry=="GB"
replace cou="NOR" if cntry=="NO"
replace cou="AUT" if cntry=="AT"
replace cou="ITA" if cntry=="IT"

gen iscoco1=real(substr(string(iscoco),1,1)) // Creating occupational 1-digit identifier

merge m:1 cou year iscoco1 using rehm
drop if _merge==2
drop _merge

* Remove some cases

* drop respondents under 18 (or 123) and with missing gender

drop if age < 18
drop if age == 123
drop if gender == .

* drop missing age

drop if age==.

* Saving data as a working file 

save rueda, replace

